package io.lkt.modules.sys.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.lkt.common.util.Query;
import io.lkt.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lkt.modules.sys.entity.BConnectionApplyInfoEntity;
import io.lkt.modules.sys.service.BConnectionApplyInfoService;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;



/**
 * 人脉广场添加好友申请表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2021-09-29 17:40:38
 */
@RestController
@RequestMapping("/sys/bconnectionapplyinfo")
public class BConnectionApplyInfoController {
    @Autowired
    private BConnectionApplyInfoService bConnectionApplyInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:bconnectionapplyinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bConnectionApplyInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:bconnectionapplyinfo:info")
    public R info(@PathVariable("id") Integer id){
        BConnectionApplyInfoEntity bConnectionApplyInfo = bConnectionApplyInfoService.getById(id);

        return R.ok().put("bConnectionApplyInfo", bConnectionApplyInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:bconnectionapplyinfo:save")
    public R save(@RequestBody BConnectionApplyInfoEntity bConnectionApplyInfo){
        bConnectionApplyInfoService.save(bConnectionApplyInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:bconnectionapplyinfo:update")
    public R update(@RequestBody BConnectionApplyInfoEntity bConnectionApplyInfo){
        ValidatorUtils.validateEntity(bConnectionApplyInfo);
        bConnectionApplyInfoService.updateById(bConnectionApplyInfo);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:bconnectionapplyinfo:delete")
    public R delete(@RequestBody Integer[] ids){
        bConnectionApplyInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/listByUserId")
    public R listByUserId(@RequestParam Map<String, Object> params) {
        if (params.get("userId") == null) return R.error("用户不存在");
        Query query = new Query(params);
        List<BConnectionApplyInfoEntity> list = bConnectionApplyInfoService.getListByUserId(query);
        int total = bConnectionApplyInfoService.getListByUserIdTotal(query).intValue();

        PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page", page);
    }

    @RequestMapping("/getUserConnectionApplyCount/{userId}")
    public R getUserConnectionApplyCount(@PathVariable("userId") Integer userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        int total = bConnectionApplyInfoService.getListByUserIdTotal(params).intValue();
        return R.ok().put("userConnectionApplyCount", total);
    }

}
